//package medicalApp.velociraptors.com;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import javax.swing.table.AbstractTableModel;

/**
 * @author Lsun
 *
 */
@SuppressWarnings("serial")
public class LookupTableModelAdmin extends AbstractTableModel {
	private Object[][] data;
	private String columnNames[] = {"First Name", "Last Name", "Social Security Number", "User type"};
	private MedAppController mac;
	private Connection con;
	ArrayList<String> patientFirstNames;
	ArrayList<String> patientLastNames;
	ArrayList<Integer> patientSSN;
	ArrayList<String> type;
	private int socialSN;
	private String lastName;
	private String firstName;
	private boolean boolFirstName;
	private boolean boolLastName;
	private boolean boolSSN;

	public LookupTableModelAdmin(final MedAppController mac, String firstName2, String lastName2, int socialSN2){
		firstName = firstName2;
		mac.out.println("Lookup First Name: " + firstName2);
		lastName = lastName2;
		mac.out.println("Lookup Last Name: " + lastName2);
		socialSN = socialSN2;
		mac.out.println("Lookup SSN: " + socialSN2);

		boolFirstName = !firstName.equals("");
		mac.out.println("First Name not is null: " + boolFirstName);
		boolLastName = !lastName.equals("");
		mac.out.println("Last Name not is null: " + boolLastName);
		boolSSN = socialSN2 != 0;
		mac.out.println("SSN not is null: " + boolSSN);

		this.mac = mac;
		con = mac.getCon();
		patientFirstNames = new ArrayList<String>();
		patientLastNames = new ArrayList<String>();
		patientSSN = new ArrayList<Integer>();
		type  = new ArrayList<String>();
		
		String sql="";
		if(boolFirstName && boolLastName && boolSSN){
			sql = "SELECT * FROM `user` WHERE `U_FIRSTNAME` ='" + firstName + "' AND `U_LASTNAME`= '" + lastName + "' AND `U_SSN`= '" + socialSN + "'";
		}
		else if(boolFirstName && !boolLastName && boolSSN){
			sql = "SELECT * FROM `user` WHERE `U_FIRSTNAME` ='" + firstName + "' AND `U_SSN`= '" + socialSN + "'";
		}
		else if(boolFirstName && boolLastName && !boolSSN){
			sql = "SELECT * FROM `user` WHERE `U_FIRSTNAME` ='" + firstName + "' AND `U_LASTNAME`= '" + lastName + "'";
		}
		else if(!boolFirstName && boolLastName && boolSSN){
			sql = "SELECT * FROM `user` WHERE `U_LASTNAME`= '" + lastName + "' AND `U_SSN`= '" + socialSN + "'";
		}
		else if(boolFirstName && !boolLastName && !boolSSN){
			sql = "SELECT * FROM `user` WHERE `U_FIRSTNAME` ='" + firstName + "'";
		}
		else if(!boolFirstName && boolLastName && !boolSSN){
			sql = "SELECT * FROM `user` WHERE `U_LASTNAME`= '" + lastName + "'";
			
		}
		else if(!boolFirstName && !boolLastName && boolSSN){
			sql = "SELECT * FROM `user` WHERE `U_SSN`= '" + socialSN + "'";
		}
		else if(!(boolFirstName && boolLastName && boolSSN)){
			sql = "SELECT * FROM `user`";
		}

		try {
			Statement stmt = con.createStatement();
			ResultSet rs = stmt.executeQuery(sql);
			while(rs.next()){
				patientFirstNames.add(rs.getString("U_FIRSTNAME"));
				patientLastNames.add(rs.getString("U_LASTNAME"));
				patientSSN.add(rs.getInt("U_SSN"));
				type.add(rs.getString("USERTYPE"));
			}

//			if(boolFirstName){
//				boolean success = false;
//				int count = 0;
//				while(!success){
//					if(!(patientFirstNames.get(count).equals(firstName))){
//						patientFirstNames.remove(count);
////						count++;
//					}
//					else
//						count++;
//					
//					if(patientFirstNames.size()==1)
//						success=true;
//				}
//			}
//
//			if(boolLastName){
//				boolean success = false;
//				int count = 0;
//				while(!success){
//					if(!(patientLastNames.get(count).equals(lastName))){
//						patientLastNames.remove(count);
////						count++;
//					}
//					else
//						count++;
//					
//					if(patientLastNames.size()==1)
//						success=true;
//				}
//			}
//
//			if(boolDOB){
//				boolean success = false;
//				int count = 0;
//				while(!success){
//					if(!(patientDOB.get(count).equals(dateOfBirth))){
//						patientDOB.remove(count);
////						count++;
//					}
//					else
//						count++;
//					
//					if(patientDOB.size()==1)
//						success=true;
//				}
//			}
//			
//			if(boolSSN){
//				boolean success = false;
//				int count = 0;
//				while(!success){
//					if(!(patientSSN.get(count)==socialSN)){
//						patientSSN.remove(count);
////						count++;
//					}
//					else
//						count++;
//					
//					if(patientSSN.size()==1)
//						success=true;
//				}
//			}

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		data = new Object[patientFirstNames.size()][4];

		for(int i = 0; i<patientFirstNames.size();i++){
			data[i][0] = patientFirstNames.get(i);
		}

		for(int i = 0; i<patientLastNames.size();i++){
			data[i][1] = patientLastNames.get(i);
		}

		for(int i = 0; i<patientSSN.size();i++){
			data[i][2] = patientSSN.get(i);
		}
		
		for(int i = 0; i<type.size();i++){
			data[i][3] = type.get(i);
		}
	}
	
	private int minimum(int a, int b, int c, int d){
		int min = Math.min(Math.min(a,b), Math.min(c,d));
		return min;
	}
	/* (non-Javadoc)
	 * @see javax.swing.table.TableModel#getColumnCount()
	 */
	@Override
	public int getColumnCount() {
		return columnNames.length;
	}

	/* (non-Javadoc)
	 * @see javax.swing.table.TableModel#getRowCount()
	 */
	@Override
	public int getRowCount() {
		// TODO Auto-generated method stub
		return data.length;
	}

	/* (non-Javadoc)
	 * @see javax.swing.table.TableModel#getValueAt(int, int)
	 */
	@Override
	public Object getValueAt(int rowIndex, int columnIndex){
		// TODO Auto-generated method stub
		//		if(data[rowIndex][columnIndex]==null){
		//			return null;
		//		}
		return data[rowIndex][columnIndex];
	}

	public String getColumnName(int col) {
		return columnNames[col];
	}
}
